package com.crdeng.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crdeng.business.entity.InStock;
import com.crdeng.business.entity.Supplier;
import com.crdeng.business.mapper.SupplierMapper;
import com.crdeng.business.service.InStockService;
import com.crdeng.business.service.SupplierService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crdeng.business.vo.SupplierQueryMap;
import com.crdeng.business.vo.SupplierVO;
import com.crdeng.handler.BusinessException;
import com.crdeng.response.ResultCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author crdeng
 * @since 2021-01-07
 */
@Service

public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements SupplierService {

    @Autowired
    private InStockService inStockService;

    @Override
    public Supplier add(SupplierVO supplierVO) {

        Date date = new Date();
        supplierVO.setCreateTime(date);
        supplierVO.setModifiedTime(date);
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        boolean save = save(supplier);
        if (!save){
            throw new BusinessException(ResultCode.COMMON_FAIL.getCode(),"添加供应商失败");

        }
        return supplier;
    }

    @Override
    public IPage<Supplier> findSupplier(Integer pageNun, Integer pageSize, SupplierQueryMap queryMap) {
        Page<Supplier> supplierPage = new Page<>(pageNun, pageSize);
        QueryWrapper<Supplier> supplierQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(queryMap.getAddress())){
            supplierQueryWrapper.like("address",queryMap.getAddress());
        }
        if (!StringUtils.isEmpty(queryMap.getContact())){
            supplierQueryWrapper.like("contact",queryMap.getContact());
        }
        if (!StringUtils.isEmpty(queryMap.getName())){
            supplierQueryWrapper.like("name",queryMap.getName());
        }

        return baseMapper.selectPage(supplierPage,supplierQueryWrapper);
    }

    /*删除*/
    @Override
    public void deleteSupplierById(Long id) {
        Supplier byId = getById(id);
        if (byId==null){
            throw  new BusinessException(ResultCode.PARAM_IS_BLANK.getCode(),"暂无查询到此供应商");
        }
        QueryWrapper<InStock> inStockQueryWrapper = new QueryWrapper<>();
        inStockQueryWrapper.eq("supplier_id",id);
        List<InStock> list = inStockService.list(inStockQueryWrapper);
        if (list.size()>=1){
            throw  new BusinessException(ResultCode.COMMON_FAIL.getCode(),"供应商存在入库单联系，无法直接删除");
        }
        removeById(id);
    }

    /*编辑*/
    @Override
    public void editSupplierById(SupplierVO supplierVO) {
        Supplier byId = getById(supplierVO.getId());
        if (byId==null){throw  new BusinessException(ResultCode.PARAM_NOT_VALID.getCode(),"供应商不存在");}
        Supplier supplier = new Supplier();
        supplier.setModifiedTime(new Date());
        BeanUtils.copyProperties(supplierVO,supplier);
        baseMapper.updateById(supplier);

    }
}
